package com.ruoyi.web.controller.module.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.market.domain.ConfigBase;
import com.ruoyi.market.service.IConfigBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 店铺基本配置信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/market/configBase")
public class ConfigBaseController extends BaseController
{
    @Autowired
    private IConfigBaseService configBaseService;

    /**
     * 获取店铺基本配置列表
     */
    @PostMapping("/configBaseList")
    public TableDataInfo list(@RequestBody ConfigBase configBase)
    {
        startPageByPost();
        List<ConfigBase> list = configBaseService.selectConfigBaseList(configBase);
        return getDataTable(list);
    }

    /**
     * 根据店铺基本配置编号获取详细信息
     */
    @PostMapping("/getConfigBase")
    public AjaxResult getConfigBase(HttpServletRequest request, @RequestBody Map<String, Object> params)
    {
        String shopId_header = request.getHeader("ShopId"); // 店铺id
        String shopId_body = params.get("shopId").toString(); // 店铺id
        if ((shopId_header == null || shopId_header.equals("")) && (shopId_body == null || shopId_body.equals(""))) {
            return error("店铺基本配置失败，参数错误");
        }
        String shopId = shopId_body;
        if(shopId_header != null && !shopId_header.equals("")){
            shopId = shopId_header;
        }
        AjaxResult ajax = AjaxResult.success();
        ConfigBase sysConfigBase = configBaseService.selectConfigBaseByShopId(shopId);
        ajax.put(AjaxResult.DATA_TAG, sysConfigBase);
        return ajax;
    }

    /**
     * 新增店铺基本配置
     */
    @PreAuthorize("@ss.hasPermi('market:configBase:add')")
    @Log(title = "店铺基本配置管理 - 新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ConfigBase configBase)
    {
        configBase.setCreateBy(getUsername());
        return toAjax(configBaseService.insertConfigBase(configBase));
    }

    /**
     * 修改店铺基本配置
     */
    @PreAuthorize("@ss.hasPermi('market:configBase:edit')")
    @Log(title = "店铺基本配置管理 - 修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ConfigBase configBase)
    {
        configBase.setUpdateBy(getUsername());
        return toAjax(configBaseService.updateConfigBase(configBase));
    }

    /**
     * 保存店铺基本配置
     */
    @PostMapping("/save")
    public AjaxResult save(@Validated @RequestBody ConfigBase configBase)
    {
        if(StringUtils.isNotNull(configBase) && StringUtils.isNotNull(configBase.getShopId())){
            ConfigBase cb = configBaseService.selectConfigBaseByShopId(configBase.getShopId());
            if(StringUtils.isNotNull(cb)){
                configBaseService.updateConfigBase(configBase);
            }else{
                configBaseService.insertConfigBase(configBase);
            }
        }else {
            return error("保存店铺基本配置失败，参数错误");
        }
        configBase.setCreateBy(getUsername());
        configBase.setUpdateBy(getUsername());
        return toAjax(configBaseService.updateConfigBase(configBase));
    }

    /**
     * 删除店铺基本配置
     */
    @PreAuthorize("@ss.hasPermi('market:configBase:remove')")
    @Log(title = "店铺基本配置管理 - 删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configBaseIds}")
    public AjaxResult remove(@PathVariable Long[] configBaseIds)
    {
        return toAjax(configBaseService.deleteConfigBaseByIds(configBaseIds));
    }
}
